// VR影院页面功能
(function () {
    var isLoading = false;
    
    // 初始化页面
    function initialize() {
        loadEquipmentStatus();
        loadQueueInfo();
        setupEventListeners();
        
        // 每5秒更新一次设备状态
        setInterval(loadEquipmentStatus, 5000);
        // 每3秒更新一次排队信息
        setInterval(loadQueueInfo, 3000);
    }
    
    // 加载设备状态
    function loadEquipmentStatus() {
        if (isLoading) return;
        isLoading = true;
        
        showLoadingState('equipment');
        
        apiService.getVRCinemaEquipment()
            .then(function (data) {
                console.log('VR设备状态数据:', data);
                updateEquipmentStatus(data);
                hideLoadingState('equipment');
            })
            .catch(function (err) {
                console.error('获取VR设备状态失败:', err);
                hideLoadingState('equipment');
                showEquipmentError();
            })
            .finally(function() {
                isLoading = false;
            });
    }
    
    // 更新设备状态显示
    function updateEquipmentStatus(data) {
        var vrDeviceStatusEl = document.getElementById('vrDeviceStatus');
        
        if (vrDeviceStatusEl) {
            var status = data.status || 'available';
            var statusText = getEquipmentStatusText(status);
            var statusClass = getEquipmentStatusClass(status);
            
            vrDeviceStatusEl.textContent = statusText;
            vrDeviceStatusEl.className = 'equipment-status ' + statusClass;
        }
    }
    
    // 获取设备状态文本
    function getEquipmentStatusText(status) {
        switch (status) {
            case 'available': return '可用';
            case 'in_use': return '使用中';
            case 'maintenance': return '维护中';
            case 'error': return '故障';
            default: return '未知';
        }
    }
    
    // 获取设备状态样式类
    function getEquipmentStatusClass(status) {
        switch (status) {
            case 'available': return 'available';
            case 'in_use': return 'in-use';
            case 'maintenance': return 'maintenance';
            case 'error': return 'error';
            default: return 'unknown';
        }
    }
    
    // 加载排队信息
    function loadQueueInfo() {
        apiService.getVRCinemaQueue()
            .then(function (data) {
                console.log('排队信息数据:', data);
                updateQueueInfo(data);
            })
            .catch(function (err) {
                console.error('获取排队信息失败:', err);
                showQueueError();
            });
    }
    
    // 更新排队信息显示
    function updateQueueInfo(data) {
        var queueCountEl = document.getElementById('queueCount');
        var waitTimeEl = document.getElementById('waitTime');
        var projectStatusEl = document.getElementById('projectStatus');
        
        if (queueCountEl) {
            queueCountEl.textContent = data.queueCount || 0;
        }
        
        if (waitTimeEl) {
            waitTimeEl.textContent = data.waitTime ? data.waitTime + '分钟' : '计算中...';
        }
        
        if (projectStatusEl) {
            var status = data.status || 'running';
            projectStatusEl.textContent = getProjectStatusText(status);
            projectStatusEl.className = 'project-status ' + status;
        }
    }
    
    // 获取项目状态文本
    function getProjectStatusText(status) {
        switch (status) {
            case 'running': return '运行中';
            case 'maintenance': return '维护中';
            case 'closed': return '已关闭';
            case 'error': return '故障';
            default: return '未知';
        }
    }
    
    // 显示加载状态
    function showLoadingState(type) {
        if (type === 'equipment') {
            var equipmentItems = document.querySelectorAll('.equipment-item');
            equipmentItems.forEach(function(item) {
                item.classList.add('loading');
            });
        }
    }
    
    // 隐藏加载状态
    function hideLoadingState(type) {
        if (type === 'equipment') {
            var equipmentItems = document.querySelectorAll('.equipment-item');
            equipmentItems.forEach(function(item) {
                item.classList.remove('loading');
            });
        }
    }
    
    // 显示设备错误状态
    function showEquipmentError() {
        var vrDeviceStatusEl = document.getElementById('vrDeviceStatus');
        
        if (vrDeviceStatusEl) {
            vrDeviceStatusEl.textContent = '连接失败';
            vrDeviceStatusEl.className = 'equipment-status error';
        }
    }
    
    // 显示排队错误状态
    function showQueueError() {
        var queueCountEl = document.getElementById('queueCount');
        var waitTimeEl = document.getElementById('waitTime');
        var projectStatusEl = document.getElementById('projectStatus');
        
        if (queueCountEl) {
            queueCountEl.textContent = '获取失败';
            queueCountEl.style.color = '#dc3545';
        }
        
        if (waitTimeEl) {
            waitTimeEl.textContent = '无法获取';
            waitTimeEl.style.color = '#dc3545';
        }
        
        if (projectStatusEl) {
            projectStatusEl.textContent = '连接失败';
            projectStatusEl.className = 'project-status error';
        }
    }
    
    // 设置事件监听器
    function setupEventListeners() {
        // 可以添加其他事件监听器
    }
    
    // 页面加载时初始化
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function () {
            initialize();
        });
    } else {
        initialize();
    }
})();
